from typing import Any

from aiogram import F, types
from aiogram_dialog import DialogManager, Window
from aiogram_dialog.widgets.kbd import Back, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Format

from app.bot.dialog.dialog_states import SelectProxySG
from app.bot.dialog.getters.providers import providers_getter
from app.bot.dialog.widgets import LocaleText


async def set_proxy_provider(
    call: types.CallbackQuery, widget: Any, manager: DialogManager, provider_id: str
):
    manager.dialog_data["provider_id"] = provider_id

    if manager.start_data and manager.start_data.get("for_test"):
        manager.dialog_data["period"] = "twenty_minutes"
        await manager.switch_to(SelectProxySG.set_order_quantity)
    elif manager.dialog_data.get("proxy_privacy") == "private":
        await manager.switch_to(SelectProxySG.select_period)
    else:
        await manager.switch_to(SelectProxySG.list_of_categories)


proxy_providers_window = Window(
    LocaleText("select-proxy-provider"),
    ScrollingGroup(
        Select(
            Format("{item.name}"),
            "catselsel",
            lambda provider: provider.id,
            "providers",
            on_click=set_proxy_provider,
        ),
        width=2,
        height=4,
        id="catsel",
        when=F["user_language"] == "ru",
    ),
    ScrollingGroup(
        Select(
            Format("{item.name_eng}"),
            "catselsel",
            lambda provider: provider.id,
            "providers",
            on_click=set_proxy_provider,
        ),
        width=2,
        height=4,
        id="catsel",
        when=F["user_language"] == "en",
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.list_of_providers,
    getter=providers_getter,
)
